package com.self.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.servicebase.handler.MyException;
import com.self.serviceedu.entity.EduCourse;
import com.self.serviceedu.entity.EduCourseDescription;
import com.self.serviceedu.entity.frontvo.CourseFrontVo;
import com.self.serviceedu.entity.frontvo.CourseWebVo;
import com.self.serviceedu.entity.vo.CourseInfoVo;
import com.self.serviceedu.entity.vo.CoursePublishVo;
import com.self.serviceedu.mapper.EduCourseMapper;
import com.self.serviceedu.service.EduChapterService;
import com.self.serviceedu.service.EduCourseDescriptionService;
import com.self.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1. 向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0){
            //添加失败
            throw new MyException(20001,"添加课程信息失败");
        }

        String courseId = eduCourse.getId();

        //2. 向课程简介表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(courseDescription);

        return courseId;
    }

    //根据课程Id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1. 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //2.查询课程描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        System.out.println(eduCourse);
        System.out.println(courseDescription);

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程基本信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        //1. 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new MyException(20001,"修改课程信息失败");
        }

        //2. 修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourseById(String courseId) {

        //1. 根据课程Id删除小节
        videoService.removeVideoByCourseId(courseId);

        //2. 根据课程Id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3. 根据课程Id删除描述
        courseDescriptionService.removeById(courseId);

        //4. 根据课程Id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0){
            throw  new MyException(20001,"删除失败~~");
        }
    }

    //前台课程分页及模糊查询
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//最新
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);//已经查询封装进了pageCourse

        List<EduCourse> records = pageCourse.getRecords();//每页数据的List集合
        long current = pageCourse.getCurrent();//当前页
        long pages = pageCourse.getPages();//总页数
        long size = pageCourse.getSize();//每页记录数
        long total = pageCourse.getTotal();//总记录数
        boolean hasNext = pageCourse.hasNext();//下一页
        boolean hasPrevious = pageCourse.hasPrevious();//前一页

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //1.根据课程Id，编写SQL语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
